import 'dart:convert';

import 'package:freemusic/model/AbstractModel.dart';
import 'package:freemusic/model/Lyric.dart';

class SongPlatform {
  static final String qq = "qq";
  static final String kugou = "kugou";
  static final String netease = "netease";
  static final String local = "local";
  static final String migu = "migu";
}


abstract class AbstractSong extends AbstractModel{
  String hash;    //类似ID，歌曲唯一标识，各个平台可能字段不一致，这里统一表示为hash
  String songName;
  String singerName;
  String platform ;    //歌曲属于某个平台类型为 SongPlatform, 如果为 "local", 为本地音乐。
  String lyrics;
  String playUrl;
  String img;
  Duration timeLength ; //时间
  String type;        //文件格式mp3，m4a等
  bool isLocal = false;    //是否为本地音乐

  //从服务器json中写入歌曲信息
  List<AbstractSong> fromRemoteJosn(Map json);

  //字符串转歌词
  Lyric stringToLyric();

  String toJsonString(){
    return jsonEncode(toJsonMap());
  }

  //将歌曲信息转成Json
  Map toJsonMap(){
    Map map = Map();
    if(this.id == null){
      id = 0;
    }
    map['id'] = this.id;
    map['img'] = this.img;
    map['hash'] = this.hash;
    map['songName'] = this.songName;
    map['singerName'] = this.singerName;
    map['platform'] = this.platform;
    map['playUrl'] = this.playUrl;
    map['timeLength'] = this.timeLength.inMilliseconds;
    map['type'] = this.type;
    map['isLocal'] = this.isLocal;
    return map;
  }

  void fromDataBase(Map map){
    if(map['id'] == null){
      map['id'] = 0;
    }
    this.id = int.parse(map['id'].toString());
    this.playUrl = map['playUrl'];
    this.img = map['img'];
    this.timeLength = new Duration(milliseconds: map['timeLength']);
    this.singerName = map['singerName'];
    this.hash = map['hash'];
    this.songName = map['songName'];
    this.type = map['type'];
    this.platform = map['platform'];
    this.isLocal = map['isLocal'];
    this.singerName = map['singerName'];
    if(this.isLocal == null){
      if (platform == SongPlatform.local) {
        this.isLocal = true;
      } else {
        this.isLocal = false;
      }
    }
    if(img == "null"){
      this.img = null;
    }
  }

  @override
  String toString() {
    return "{\"playUrl\":\"$playUrl\","+
           "\"img\":\"$img\","+
          //  "\"lyrics\":"+ """$lyrics"""+
           "\"timeLength\":${this.timeLength.inMilliseconds},"+
           "\"singerName\":\"$singerName\","+
           "\"songName\":\"$songName\","+
           "\"hash\":\"$hash\","+
           "\"type\":\"$type\","+
           "\"platform\":\"$platform\""+
           "}";
  }

  Map<String, dynamic> toMap(){
    return jsonDecode(this.toString());
  }

}